package org.asiainfo.file.service.impl;

import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.core.utils.file.FileUploadUtils;
import org.asiainfo.common.core.utils.file.FileUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.file.domain.AttactmentInfo;
import org.asiainfo.file.domain.vo.AttactmentInfoVo;
import org.asiainfo.file.domain.bo.AttactmentInfoBo;
import org.asiainfo.file.service.AttactmentInfoService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.file.mapper.AttactmentInfoMapper;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Collection;
import java.util.List;

/**
 * 附件文件详细信息(AttactmentInfo)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AttactmentInfoServiceImpl extends ServiceImpl<AttactmentInfoMapper, AttactmentInfo> implements AttactmentInfoService {

    /**
     * 查询
     *
     * @param attactmentInfoBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<AttactmentInfoVo> queryList(AttactmentInfoBo attactmentInfoBo) {
        Map<String, Object> params = attactmentInfoBo.getParams();
        QueryWrapper query = query();
        return listAs(query, AttactmentInfoVo.class);
    }

    /**
     * 通过attactmentInfoId查询单条数据
     *
     * @param attactmentInfoId 主键
     * @return 实例对象
     */
    @Override
    public AttactmentInfoVo queryById(Long attactmentInfoId) {
        return getOneAs(query().eq(AttactmentInfo::getId, attactmentInfoId), AttactmentInfoVo.class);
    }

    /**
     * 分页查询
     *
     * @param attactmentInfoBo 筛选条件
     * @param pageQuery        分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<AttactmentInfoVo> queryPageList(AttactmentInfoBo attactmentInfoBo, PageQuery pageQuery) {
        Map<String, Object> params = attactmentInfoBo.getParams();
        QueryWrapper query = query().between(AttactmentInfo::getCreateTime, params.get("beginTime"), params.get("endTime"),
                ObjectUtil.isNotNull(params.get("beginTime")) && ObjectUtil.isNotNull(params.get("endTime")));
        pageQuery.buildOrder(query);
        query.eq(AttactmentInfo::getAttactId, attactmentInfoBo.getAttactId(), ObjectUtil.isNotNull(attactmentInfoBo.getAttactId()));
        query.like(AttactmentInfo::getFileName, attactmentInfoBo.getFileName(), StringUtils.isNotBlank(attactmentInfoBo.getFileName()));
        query.eq(AttactmentInfo::getUploadType, attactmentInfoBo.getUploadType(), ObjectUtil.isNotNull(attactmentInfoBo.getUploadType()));
        Page<AttactmentInfoVo> page = pageAs(pageQuery.buildPage(), query, AttactmentInfoVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param attactmentInfoBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(AttactmentInfoBo attactmentInfoBo) {
        AttactmentInfo attactmentInfo = MapstructUtils.convert(attactmentInfoBo, AttactmentInfo.class);
        return save(attactmentInfo);
    }

    /**
     * 修改数据
     *
     * @param attactmentInfoBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(AttactmentInfoBo attactmentInfoBo) {
        AttactmentInfo attactmentInfo = MapstructUtils.convert(attactmentInfoBo, AttactmentInfo.class);
        return updateById(attactmentInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param attactmentInfoIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> attactmentInfoIds) {
        return removeByIds(attactmentInfoIds);
    }

    /**
     * 构建附件详细信息
     *
     * @param attactmentId 附件ID
     * @param fileUrl      文件URL
     * @param file         文件
     * @return 构建结果
     */
    @Override
    public AttactmentInfo buildAttactmentInfo(Long attactmentId, String fileUrl, MultipartFile file) {
        AttactmentInfo attactmentInfo = new AttactmentInfo();
        //1.获取文件类型
        attactmentInfo.setFileType(FileUploadUtils.getExtension(file));
        //2.获取文件大小
        attactmentInfo.setFileSize(file.getSize());
        //3.获取文件路径
        attactmentInfo.setUrl(fileUrl);
        //4.获取文件MD5
        attactmentInfo.setFileMd5(FileUtils.getFileMD5(file));
        //5.关联附件ID
        attactmentInfo.setAttactId(attactmentId);
        return attactmentInfo;
    }
}
